package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Pager;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.Problem;
import com.yx.changdao.common.entity.ProblemDeal;
import com.yx.changdao.common.entity.ProblemInfo;
import com.yx.changdao.common.enums.problem.Report;
import com.yx.changdao.common.utils.*;
import com.yx.changdao.common.vo.ProblemInfoVo;
import com.yx.changdao.common.vo.ProblemVo;
import com.yx.changdao.service.ProblemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 总控 逾期管理
 * @author xiapeixin
 * @date 2020-5-9
 */
@Validated
@RestController
@RequestMapping("/problem/overdue")
public class ProblemOverdueController {

    @Autowired
    protected ProblemService problemService;


    /**
     * 总控 逾期管理 未逾期
     */
    @AutoLog
    @GetMapping("/no/list")
    @RequiresPermissions("problem:overdue:list")
    @ShiroDesc("问题逾期:列表")
    public Result noList(@RequestParam("page") Integer page,
                         @RequestParam(value = "limit",required = false,defaultValue = "15") Integer limit){

        Page<Problem> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<Problem> qw = SysUtils.autoCnd(Problem.class);

        qw.orderByDesc(Problem.CREATE_TIME); //时间倒序

        qw.eq(Problem.IF_TIMEOUT,0); //未逾期
        // 获取分页结果集。
        Page<Problem> infoPage = problemService.page(pager, qw);

        List<ProblemVo> problemVos = pro2Vo(infoPage.getRecords());

        Map<String, Object> map = new HashMap<>();
        map.put("list", problemVos);
        map.put("pager", new Pager(infoPage));

        setCount(map);

        return Result.ok().data(map);
    }

    /**
     * 计算数量
     */
    private void setCount(Map<String, Object> map){
        int no = problemService.count(new QueryWrapper<Problem>().eq(Problem.IF_TIMEOUT, 0));
        int near = problemService.count(new QueryWrapper<Problem>().eq(Problem.IF_TIMEOUT, 2));
        int already = problemService.count(new QueryWrapper<Problem>().eq(Problem.IF_TIMEOUT, 1));
        map.put("no", no);
        map.put("near", near);
        map.put("already", already);
    }


    /**
     * Problem 转 ProblemVo 并计算计算剩余天数
     */
    private List<ProblemVo> pro2Vo(List<Problem> problems){
        return problems.stream().map(t -> {
            ProblemVo problemVo = new ProblemVo();
            BeanUtils.copyProperties(t, problemVo);

            //计算剩余天数
            problemVo.setLastDay(problemService.lastDay(Faster.now(),problemVo.getDealLimitTime()));

            return problemVo;
        }).collect(Collectors.toList());
    }

    /**
     * 总控 逾期管理 即将逾期
     */
    @AutoLog
    @GetMapping("/near/list")
    @RequiresPermissions("problem:overdue:list")
    @ShiroDesc("问题逾期:列表")
    public Result nearList(@RequestParam("page") Integer page,
                         @RequestParam(value = "limit",required = false,defaultValue = "15") Integer limit){

        Page<Problem> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<Problem> qw = SysUtils.autoCnd(Problem.class);

        qw.eq(Problem.IF_TIMEOUT,2); //即将逾期
        // 获取分页结果集。
        Page<Problem> infoPage = problemService.page(pager, qw);

        List<ProblemVo> problemVos = pro2Vo(infoPage.getRecords());

        Map<String, Object> map = new HashMap<>();
        map.put("list", problemVos);
        map.put("pager", new Pager(infoPage));

        setCount(map);

        return Result.ok().data(map);
    }

    /**
     * 总控 逾期管理 已逾期
     */
    @AutoLog
    @GetMapping("/already/list")
    @RequiresPermissions("problem:overdue:list")
    @ShiroDesc("问题逾期:列表")
    public Result alreadyList(@RequestParam("page") Integer page,
                           @RequestParam(value = "limit",required = false,defaultValue = "15") Integer limit){

        Page<Problem> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<Problem> qw = SysUtils.autoCnd(Problem.class);

        qw.orderByDesc(Problem.CREATE_TIME); //时间倒序

        qw.eq(Problem.IF_TIMEOUT,1); //已逾期
        // 获取分页结果集。
        Page<Problem> infoPage = problemService.page(pager, qw);

        Map<String, Object> map = new HashMap<>();
        map.put("list", infoPage.getRecords());
        map.put("pager", new Pager(infoPage));

        setCount(map);

        return Result.ok().data(map);
    }

    /**
     * 总控 逾期管理 查看
     */
    @AutoLog
    @GetMapping("/{proId}")
    public Result nearList(@PathVariable(value = "proId") Integer proId){

        Problem problem = problemService.getById(proId);
        ProblemVo problemVo = new ProblemVo();
        BeanUtils.copyProperties(problem, problemVo);
        //计算剩余天数
        problemVo.setLastDay(problemService.lastDay(Faster.now(),problemVo.getDealLimitTime()));
        return Result.ok().data(problemVo);
    }

    /**
     * type = 0 未逾期
     * type = 2 即将预期
     * type = 1 已逾期
     * @param type
     * @throws Exception
     */
    @AutoLog
    @GetMapping("/export")
    public void export(@RequestParam("type") @Range(min = 0,max = 2) Integer type,
                       @RequestParam("ids") String ids) throws Exception {

        QueryWrapper<Problem> qw = SysUtils.autoCnd(Problem.class);
        qw.eq(Problem.IF_TIMEOUT,type);
        List<Integer> idsList = Faster.batch2(ids);

        if(!idsList.isEmpty()){
            qw.in(Problem.ID,idsList);
        }

        List<ProblemVo> list = pro2Vo(problemService.list(qw));
        List<String> cloudms = new ArrayList<>();
        cloudms.add("residentName:反馈居民姓名");
        cloudms.add("category:意见建议类别");
        cloudms.add("industryCategory:意见建议行业类别");

        if(!type.equals(1)){
            cloudms.add("lastDay:剩余办理天数");
        }

        cloudms.add("content:意见建议内容");
        cloudms.add("depName:承办部门");
        cloudms.add("difficultLevel:办理难度");
        ExcelImportExportUtils.writeExcelByCloum(list,SysUtils.getResponse(),"导出",cloudms);
    }

}
